﻿using System;

public static class SysNumerical
{
    private static string moneyformat = "{0:C2}";
    private static string numberfromat = "{0:N2}";

    public static decimal getDecimalPoints(decimal money, decimal Proportion, bool _decimal)
    {
        return getPoints(money, Proportion, 1M, _decimal);
    }

    public static int getIntPoints(decimal money, decimal Proportion, bool _decimal)
    {
        return (int) getPoints(money, Proportion, 1M, _decimal);
    }

    public static string GetMoneyFormatString(object value)
    {
        if (value == null)
        {
            return string.Empty;
        }
        if (string.IsNullOrEmpty(value.ToString().Trim()))
        {
            return string.Empty;
        }
        decimal _val = 0M;
        if (!decimal.TryParse(value.ToString().Trim(), out _val))
        {
            throw new Exception("abnormality occurs in the format change");
        }
        return string.Format(moneyformat, _val);
    }

    public static string GetNumberFormatString(object value)
    {
        if (value == null)
        {
            return string.Empty;
        }
        if (string.IsNullOrEmpty(value.ToString().Trim()))
        {
            return string.Empty;
        }
        decimal _val = 0M;
        if (!decimal.TryParse(value.ToString().Trim(), out _val))
        {
            throw new Exception("abnormality occurs in the format change");
        }
        return string.Format(numberfromat, _val);
    }

    public static decimal getPoints(decimal money, decimal MoneyModulus, decimal PointModulus, bool _decimal)
    {
        if (MoneyModulus == 0M)
        {
            throw new Exception("moneymodulus can not be zero");
        }
        if (MoneyModulus < 0M)
        {
            throw new Exception("moneymodulus base must be greater than zero");
        }
        if (PointModulus < 0M)
        {
            throw new Exception("pointmodulus base must be greater than zero");
        }
        if (((money == 0M) || (money < MoneyModulus)) || (PointModulus == 0M))
        {
            return 0M;
        }
        if (!_decimal)
        {
            return (PointModulus * ((money - (money % MoneyModulus)) / MoneyModulus));
        }
        return (PointModulus * (money / MoneyModulus));
    }
}

